package com.bantang.template;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.bantang.model.SandBoxParam;
import com.bantang.model.SandBoxVO;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class Test {
    public static void main(String[] args) {

        SandBoxParam sandBoxParam = new SandBoxParam();
        sandBoxParam.setLanguage("java");

        String code = ResourceUtil.readStr("testCode/Solve.java", StandardCharsets.UTF_8);

        sandBoxParam.setCode(code);
        sandBoxParam.setInput(Arrays.asList("1", "3"));

//        CodeSandBoxTemplate codeSandBoxTemplate = new JavaSandBoxTemplate();
//        SandBoxVO sandBoxVO = codeSandBoxTemplate.exec(sandBoxParam);

        SandBoxVO sandBoxVO = executeCodeSandBox(sandBoxParam);
        System.out.println(sandBoxVO);
    }

    // 定义鉴权请求头和密钥
    private static final String AUTH_REQUEST_HEADER = "auth";

    private static final String AUTH_REQUEST_SECRET = "secretKey";

    public static SandBoxVO executeCodeSandBox(SandBoxParam sandBoxParam) {
        System.out.println("远程代码沙箱");
        String url = "http://192.168.47.153:6789/executeCodeDD";
        String json = JSONUtil.toJsonStr(sandBoxParam);
        String responseStr = HttpUtil.createPost(url)
                .header(AUTH_REQUEST_HEADER, AUTH_REQUEST_SECRET)
                .body(json)
                .execute()
                .body();

        return JSONUtil.toBean(responseStr, SandBoxVO.class);
    }
}
